<?php
header("content-type:text/html;charset=utf-8");
session_start();
include "public/is_login.php";
include "../public/config/config.php";
include "../public/include/funcs.php";

$id = $_GET['id'] ? $_GET['id'] : $_SESSION['id'];
if(!$id) $id = $did;

$link=conn($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd,$cfg_dbname,$cfg_dbchar);

$arr = select($link, "user", "id=$id");
$name = $arr[0]['tname'] ? $arr[0]['tname'] : $arr[0]['nickname'];
$u_img = $arr[0]['u_img'];
$pid = $arr[0]['pid'];

$time = date("Y-m-d");
$time_arr = explode("-", $time);
$y = $time_arr[0];
$m = $time_arr[1];
$arr_num = select($link, "awa_sum","u_id=$id and y=$y and m=$m");
if($arr_num){
	$sum_a = $arr_num[0]['sum_a'];
	$sum_b = $arr_num[0]['sum_b'];
}else{
	$arr_add['u_id'] = $id;
	$arr_add['y'] = $y;
	$arr_add['m'] = $m;
	$arr_add['sum_a'] = 2;
	$arr_add['sum_b'] = 0;
	$re = add($link, "awa_sum", $arr_add);
	if($re){
		$sum_a = $arr_add['sum_a'];
		$sum_b = $arr_add['sum_b'];
	}else{
		echo "<script>alert('初始化抽奖次数出错！');</script>";
	}
}
$sum_c = $sum_a - $sum_b;
$msg = "您本月可抽奖 ".$sum_a." 次，已经抽奖 ".$sum_b." 次，剩余 ".$sum_c." 次。";
//echo $msg;

//如果是抽奖来的，未填写手机号的，需要填写手机号
if($_GET['award'] == "is"){
	$tel = $arr[0]['tel'];
	if(!$tel){
		echo "<script>";
		echo "alert('为了让您能顺利领取奖品，请填写您的姓名和手机号，以便我们和您联系。');";
		echo "window.location.href='add_name_tel.php';";
		echo "</script>";
	}
}


mysqli_close($link);
//echo "<pre>";
//print_r($arr_num);
//print_r($parr);
//echo "</pre>";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
<script src="public/jquery.min.js"></script>
<script src="public/rotaryDraw.js"></script>
<link href="/public/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="public/main.css"/>
<title>分享商城 -- 转盘抽奖</title>
<style>
*{padding:0;margin:0;}
body{
	/*background:#f03200;*/
}
.box{
	position:relative;
	max-width:750px;
	margin:0 auto;
	margin-bottom: 50px;
}
.box .img01{
	width:100%;
}
.box .drawBtn{
	position:absolute;
	width:10rem;
	height:auto;
	left:50%;
	margin-left:-5rem;
}
</style>
</head>
<body>

<div class="container bg_bai">
	<img class="list-logo" src="imgs/list/logo.png"/>
</div>

<div class="cj_div">
	<p class="msg_p"><?php echo $msg;?></p>
</div>

<div class="box">
	<img src="imgs/choujiang/img_01.png" class="img01" alt="">
	<img src="imgs/choujiang/img_03.png" class="drawBtn">
</div>

<div class="cj_div">
	<p class="cj_p">抽奖规则：抽奖每月进行一次，抽到奖后到个人中心的【我的奖品】里去查看抽到的奖，并获取奖品核验码，凭核验码到前台领取奖项。</p>
	<p class="cj_p">抽奖方法：点击抽奖转盘中心，启动抽奖程序。</p>
	<p class="cj_p">本活动解释权归武汉康承贝健口腔门诊有限公司所有。</p>
</div>

<script>

//JQ写的，稳定性不高
//$(".box .img01").load(function() {
//	var obj = $(".drawBtn");
//	var hei = $(this).height();
//	var hei2 = obj.height();
//	obj.css("top",(hei - hei2)/2);
//	console.log(hei);
//});

function top_hei(){
	var obja = document.getElementsByClassName("img01");
	var objb = document.getElementsByClassName("drawBtn");
	var heia = obja[0].height;
	var heib = objb[0].height;
	var heic = (heia - heib)/2;
	objb[0].style.top = heic+"px";
}
top_hei();
setInterval(top_hei,500);


function callbackA(ind){
	var arr = [
		"0",
		"恭喜您抽到：169元陶瓷养生锅",
		"很遗憾，请再接再厉",
		"恭喜您抽到：39元毛绒玩具",
		"恭喜您抽到：20M流量",
		"很遗憾，请再接再厉",
		"恭喜您抽到：39元毛绒玩具",
		"恭喜您抽到：20M流量",
		"很遗憾，请再接再厉"
	];
	alert(arr[ind]);
	var arra = [2,5,8];
	var index = arra.indexOf(parseInt(ind));
//	console.log(typeof(ind));
	if(index == -1){
		window.location.href="choujiang.php?award=is";
	}else{
		window.location.href="choujiang.php";
	}
};

var newdraw =new turntableDraw('.drawBtn',{
	//几份和回调函数这两个参数是必填
	share:8,	//share份额[数字没有默认],
	speed:"3s",	//speed速度[单位s,最小0.1s],
	velocityCurve:"ease",//velocityCurve速度曲线[linear匀速，ease慢快慢，ease-in慢慢开始，ease-out慢慢结束，ease-in-out慢快慢等，用的是css3的速度曲线],可以不写，ease默认值；
	weeks:6,	//weeks几周[默认2周，可以不写]
	callback:function(num){
		callbackA(num);	//callback回调函数
	},
});

$(".drawBtn").click(function(event) {
	//ajax
//	newdraw.goto(parseInt(Math.random()*8)+1);
	var sum_c = <?php echo $sum_c;?>;
	if(sum_c > 0){
		var ajax = new XMLHttpRequest();
		ajax.open('get','ajax_choujiang.php?id='+<?php echo $id;?>);
		ajax.send();
		ajax.onreadystatechange = function () {
			if (ajax.readyState==4 &&ajax.status==200) {
				var num = ajax.responseText;
				if(num == 999){
					alert("程序出错！");
				}
				if(num > 0 && num < 9){
					newdraw.goto(num);
				}
			}
		}
	}else if(sum_c == 0){
		alert("您本月的抽奖次数已经用完，请下个月再来。");
	}else{
		alert("出错啦！您的抽奖次数为："+sum_c);
	}
//	newdraw.goto(5);
});

</script>

<?php include "template/footer.php";?>
</body>
</html>
